package authentication;

import java.util.concurrent.ExecutionException;

import javax.swing.SwingWorker;

import authentication.authenticator.Authenticator;
import authentication.authenticator.Authenticator.AuthenticationResult;
import authentication.authenticator.DatabaseAuthenticator;

import components.AuthenticationContentPanel;

public class AuthenticationWorker extends SwingWorker<AuthenticationResult, Object>
{
	private final AuthenticationContentPanel panel;
	
	public AuthenticationWorker(AuthenticationContentPanel panel)
	{
		this.panel = panel;
	}
	@Override
	protected AuthenticationResult doInBackground() throws Exception
	{
		Authenticator authenticator = new DatabaseAuthenticator("jdbc:mysql://localhost:3306/Capstone", "root", "password");
		
		return authenticator.authenticate(panel.getUsername(), panel.getPassword());
	}
	@Override
	protected void done()
	{
		try
		{
			AuthenticationResult result = get();
			
			if (result.equals(AuthenticationResult.Success))
				panel.setStatusText("Login successful");
			else if (result.equals(AuthenticationResult.FailedToConnectToDatabase))
				panel.setStatusText("Could not connect to database");
			else
				panel.setStatusText("Login failed");
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			e.printStackTrace();
		}
	}

}
